package com.jlnk.rxjavademo2.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.jlnk.rxjavademo2.R;
import com.jlnk.rxjavademo2.entity.NetworkHotEntity;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
    private List<NetworkHotEntity.Result.Item> items;

    public void updateItems(List<NetworkHotEntity.Result.Item> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
        return new NewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
        NetworkHotEntity.Result.Item item = items.get(position);
        holder.titleTextView.setText(item.getTitle());
        holder.digestTextView.setText(item.getDigest());
    }

    @Override
    public int getItemCount() {
        return items != null ? items.size() : 0;
    }

    static class NewsViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        TextView digestTextView;

        NewsViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
            digestTextView = itemView.findViewById(R.id.digestTextView);
        }
    }
}